<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Model\Banner;
use App\Http\Model\System;
use App\Http\Model\Goods;
use Image;

class BannerController extends Controller
{
    //
    public function bannerList(){
        $banner = new Banner;
        $goods = New Goods;
        $bannerInfo = $banner->orderBy("sort", "asc")->get();
        $goodsInfo = $goods->get();
        return view("admin.banner.list", ["bannerInfo"=>$bannerInfo,"goodsInfo"=>$goodsInfo]);
    }
    public function bannerAdd(){
        $goods = New Goods;
        $goodsInfo = $goods->get();
        return view("admin.banner.add", ["goodsInfo"=>$goodsInfo]);
    }
    public function bannerAddRes(Request $request){
        // dd($request->all());
        $banner = new Banner;
        $bannerInfo = $banner->orderBy("sort", "desc")->first();
        if($request->hasFile("pic")){
            $banner->url = $this->uploadPic($request);
        }
        $banner->goods_id = $request->input("goods_id");
        $banner->sort = $bannerInfo->sort + 1;
        $banner->save();
        return redirect("/admin/banner-list")->with("success","轮播图添加成功");
    }
    public function bannerUpdate($id){
        $banner = new Banner;
        $goods = New Goods;
        $bannerInfo = $banner->find($id);
        $goodsInfo = $goods->get();
        return view("admin.banner.update", ["bannerInfo"=>$bannerInfo, "goodsInfo"=>$goodsInfo]);
    }
    public function bannerUpdateRes(Request $request){
        // dd($request->all());
        $banner = new Banner;
        $bannerInfo = $banner->find($request->input("id"));
        if($request->hasFile("pic")){
            @unlink("./upload".$bannerInfo->url);
            $bannerInfo->url = $this->uploadPic($request);
        }
        $bannerInfo->goods_id = $request->input("goods_id");
        $bannerInfo->save();
        return redirect("/admin/banner-list")->with("success","轮播图修改成功");
    }
    public function bannerDelete(Request $request){
        $banner = new Banner;
        $id = $request->input("banner_id");
        $banner->destroy($id);
        echo 1;die;
    }
    public function uploadPic($request){
        if($request->hasFile("pic")){
            $destinationPath = "./upload/banner/";
            $fileName = time().rand(100000,999999).'.'.$request->file("pic")->getClientOriginalExtension();
            Image::make($request->file("pic"))->resize(750, 375)->save($destinationPath.$fileName);
            return "/banner/".$fileName;die;
        }
    }
    public function bannerSort(Request $request){
        $banner = new Banner;
        $sortArr = explode(",",$request->input("sortArr"));
        $num = 1;
        foreach ($sortArr as $k => $v) {
            $bannerInfo = $banner->find($v);
            $bannerInfo->sort = $num;
            $bannerInfo->save();
            $num++;
        }
        echo 1;die;
    }

        //视频设置
    public function videoSet(){
        $video = new System;
        $videoInfo = $video->where('system_name','video')->first();
        return view('admin.banner.video',['video'=>$videoInfo]);
    }
    public function videoSetStatus(Request $request){
        $id = $request->input("id");
        $status = $request->input("status");
        $system = new System;
        $result = $system->where('id', $id)->update(['status' => $status]);
        if($result){
            echo 1;die;
        }else{
            echo 0;die;
        }
    }
}
